/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package modele;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author sio
 */
public class DAO {
    private static final String urlListeDep="http://gaemedecins.appspot.com/Controleur/medParDep/listeDep";
     private static final String urlListeMed="http://gaemedecins.appspot.com/Controleur/medParDep/listeMed/";
    public static ArrayList <String> getLesDeps(){
       ArrayList <String> l =new ArrayList <String>();
        
        try {
            
            URL myURL = new URL(urlListeDep);
            Document doc;
            DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
            DocumentBuilder db= dbf.newDocumentBuilder();
            doc= db.parse(myURL.openStream());
            Element racine =  doc.getDocumentElement();
            NodeList listeDep=racine.getElementsByTagName("Departement");
            //Recup des medecins
            for(int i=0;i<listeDep.getLength();i++){
                Node departement=listeDep.item(i);
                NodeList lesProprietes=departement.getChildNodes();
                //recherche du nom
                for(int j=0;j<lesProprietes.getLength();j++){
                    if(lesProprietes.item(j).getNodeName().equals("num")){
                        l.add(lesProprietes.item(j).getTextContent().trim());
                        break;
                    }
                }
            }
            
            
            
        } catch (MalformedURLException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        return l;
        
    }
    
    public static List <Medecin> getLesMedsParDep( String departement ){
        List <Medecin> MedsParDeps = new LinkedList <Medecin>();
        String nom = null;
        String prenom = null;
        String adresse = null;
        String specialite = null;
        String tel = null;
        
        try{
             URL myURL = new URL(urlListeMed+departement);
            Document doc;
            DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
            DocumentBuilder db= dbf.newDocumentBuilder();
            doc= db.parse(myURL.openStream());
            Element racine =  doc.getDocumentElement();
            NodeList MedsParDepartements=racine.getElementsByTagName("Medecin");
            
            for(int i=0;i<MedsParDepartements.getLength();i++){
                Node medecin=MedsParDepartements.item(i);
                NodeList lesProprietes2=medecin.getChildNodes();
                //recherche du nom
                for(int j=0;j<lesProprietes2.getLength();j++){
                    if(lesProprietes2.item(j).getNodeName().equals("nom")){
                       nom = lesProprietes2.item(j).getTextContent().trim();
                    }
                    if(lesProprietes2.item(j).getNodeName().equals("prenom")){
                       prenom = lesProprietes2.item(j).getTextContent().trim();
                    }
                    if(lesProprietes2.item(j).getNodeName().equals("adresse")){
                       adresse = lesProprietes2.item(j).getTextContent().trim();
                    }
                    if(lesProprietes2.item(j).getNodeName().equals("specialite")){
                       specialite = lesProprietes2.item(j).getTextContent().trim();
                    }
                    if(lesProprietes2.item(j).getNodeName().equals("tel")){
                       tel = lesProprietes2.item(j).getTextContent().trim();
                    }
                }
                MedsParDeps.add(new Medecin(nom, prenom, adresse, specialite, tel));
            }
        }catch (MalformedURLException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return MedsParDeps;
    }
    
}
